define(function(require) {
    var $ = require("jquery"); 
    
    function setCookie(name, value) {
        var Days = 1;
        var exp = new Date();
        exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
        document.cookie = name + "=" + escape(value) + ";Path=/;Domain=.hupu.com;expires=" + exp.toGMTString()
    }
    function getCookie(name) {
        var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
        if (arr = document.cookie.match(reg)) {
            return unescape(arr[2])
        } else {
            return null 
        }
    }
    function delCookie(name) {
        var Days = 365;
        var exp = new Date();
        exp.setTime(exp.getTime() - Days * 24 * 60 * 60 * 1000);
        var cval = getCookie(name);
        if (cval != null ) {
            document.cookie = name + "=" + cval + ";Path=/;Domain=.hupu.com;expires=" + exp.toGMTString()
        }
    }
    function logincookieHideImg(uid, tag, notJump) {
        var timeout = 1800;
        var domaincount = 0;
        for (var i = 0; i < domains.length; i++) {
            load()
        }
        function load() {
            var freeLogin = $(".checkFreeLoginTime").hasClass("notFree") ? "false" : "true";
            var timeElapsed = false;
            var imagesTimer = setTimeout(function() {
                timeElapsed = true;
                done()
            }
            , timeout);
            $("<img />").attr("src", getRealDomain(domains[i]) + "/m/2/login/crossdomain?uid=" + uid + "&freeLogin=" + freeLogin + "&tag=" + tag).load(function() {
                imgload()
            }
            ).error(function() {
                imgload()
            }
            );
            function imgload() {
                if (!timeElapsed) {
                    clearTimeout(imagesTimer);
                    done()
                }
            }
        }
        function done() {
            domaincount++;
            if (domaincount >= domains.length) {
                if (notJump == null  || notJump != true) {
                    jumpurlsubmit()
                }
            }
        }
    }

    function qqLogincookieHideImg(uid, jumpUrl, tag) {
        var timeout = 1800;
        var domaincount = 0;
        for (var i = 0; i < domains.length; i++) {
            load()
        }
        function load() {
            var freeLogin = $(".checkFreeLoginTime").hasClass("notFree") ? "false" : "true";
            var timeElapsed = false;
            var imagesTimer = setTimeout(function() {
                timeElapsed = true;
                done()
            }
            , timeout);
            $("<img />").attr("src", getRealDomain(domains[i]) + "/m/2/login/crossdomain?uid=" + uid + "&freeLogin=" + freeLogin + "&tag=" + tag).load(function() {
                imgload()
            }
            ).error(function() {
                imgload()
            }
            );
            function imgload() {
                if (!timeElapsed) {
                    clearTimeout(imagesTimer);
                    done()
                }
            }
        }
        function done() {
            domaincount++;
            if (domaincount >= domains.length) {
                window.parent.location.href = jumpUrl
            }
        }
    }
    function logoutCleanCookie() {
        var timeout = 1800;
        var domaincount = 0;
        for (var i = 0; i < domains.length; i++) {
            load()
        }
        function load() {
            var timeElapsed = false;
            var imagesTimer = setTimeout(function() {
                timeElapsed = true;
                done()
            }
            , timeout);
            $("<img />").attr("src", getRealDomain(domains[i]) + "/m/2/logout/crossdomain").load(function() {
                imgload()
            }
            ).error(function() {
                imgload()
            }
            );
            function imgload() {
                if (!timeElapsed) {
                    clearTimeout(imagesTimer);
                    done()
                }
            }
        }
        function done() {
            domaincount++;
            if (domaincount >= domains.length) {
                jumpurlsubmit()
            }
        }
    }
    function jumpurlsubmit() {
        var form = document.createElement("form");
        var formName = "jumpform";
        form.style.display = "none";
        form.action = "/pc/redirectJumpUrl";
        form.name = formName;
        document.body.appendChild(form);
        form.submit()
    }
    function ucjump(funcName) {
        var form = document.createElement("form");
        var formName = "jumpform";
        form.style.display = "none";
        form.action = curDomain + "/ucenter/redirectJumpUrl?funcion=" + funcName;
        form.name = formName;
        form.method = "GET";
        document.body.appendChild(form);
        var tempInput = document.createElement("input");
        tempInput.type = "hidden";
        tempInput.name = "funcion";
        tempInput.value = funcName;
        form.appendChild(tempInput);
        form.submit()
    }
    
    function getRealDomain(domain) {
        var schema = window.currentSchema;
        if(!window.currentSchema) {
            schema = "http://"; 
        }
        
        // haitaodashi强制降级http
        if(domain.indexOf("haitaodashi")>-1){
        	schema = "http://"; 
        }
        
        return schema + domain;
    }

    return {
        setCookie: setCookie,
        getCookie: getCookie,
        delCookie: delCookie,
        ucjump: ucjump,
        logincookieHideImg: logincookieHideImg,
        logoutCleanCookie: logoutCleanCookie
    }

});